package view;
/**
 *
 * @author zhaoyingxi
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;
import java.util.List;
import model.AirLine;
import Control.AirLineControl;
import javax.swing.JComponent;
/**
@author zhaoyingxi jc11839209
*/
public class AirLineManagerViewT {
    private JFrame frame = null;

    private JTable table = null;

    private Table_Model model = null;

    private JScrollPane s_pan = null;

    private JButton button_1 = null, button_2 = null, button_3 = null;

    private JPanel pane = null;
    public AirLineManagerViewT() {

        AirLineControl airLineControl = new AirLineControl();
        List<AirLine> list = airLineControl.find();
        frame = new JFrame("AirLineManagerView");

        pane = new JPanel();

        button_1 = new JButton("Delete");

        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                removeData();
            }
        });
        button_2 = new JButton("ADD");

        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addLine();
//                addData();
            }
        });

        button_3 = new JButton("SAVE");
        button_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                saveData();
            }
        });
        pane.add(button_1);
        pane.add(button_2);
        pane.add(button_3);
        model = new Table_Model(20);
        table = new JTable(model);
        //
        table.setBackground(Color.white);

        TableColumnModel tcm = table.getColumnModel();

        tcm.getColumn(0).setPreferredWidth(50);
        tcm.getColumn(1).setPreferredWidth(100);
        tcm.getColumn(2).setPreferredWidth(50);
        for(int i=0;i<list.size();i++)
        {
            this.addData(list.get(i));
        }
        s_pan = new JScrollPane(table);
        frame.add(s_pan, BorderLayout.CENTER);
        frame.add(s_pan, BorderLayout.CENTER);
        frame.add(pane, BorderLayout.NORTH);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        frame.setSize(frame.getMaximumSize());
        frame.setResizable(false);
        frame.setVisible(true);
    }
    private void addData(AirLine airline) {
    model.addRow(airline.getAirLineNum()+"", airline.getFrom()+"",  airline.getTo()+"",airline.getPrice()+""
                     , airline.getDiscountPrice()+"",  airline.getDepartureTime()+"",airline.getSoldNum()+""
                     ,airline.getMax()+"", airline.getDiscountNum()+"", airline.getSumMoney()+"");

        table.updateUI();
    }
    private void addLine(){
        model.addRow("AirLineNum", "From","To","Price"
                     , "DiscountPrice",  "DepartureTime","SoldNum"
                     ,"Max","DiscountNum", "SumMoney");
        table.updateUI();
    }
    private void removeData() {
             int a =  table.getSelectedRow();
             String num = (String) model.getValueAt(a,0);
             System.out.print(num);
             AirLineControl airLineControl = new AirLineControl();
             airLineControl.deleteAirLine(num);
             model.removeRow(a);
             table.updateUI();
//        model.removeRows(0, model.getRowCount());
//
//        table.updateUI();
    }
    //
    private void saveData() {
        Integer row = table.getSelectedRow();
        String airLineNum = (String) model.getValueAt(row, 0);
        String from = (String) model.getValueAt(row, 1);
        String to = (String) model.getValueAt(row, 2);
        String price = (String) model.getValueAt(row, 3);
        String discountPrice = (String) model.getValueAt(row, 4);
        String departureTime = (String) model.getValueAt(row, 5);
        String soldNum = (String) model.getValueAt(row, 6);
        String max = (String) model.getValueAt(row, 7);
        String discountNum = (String) model.getValueAt(row, 8);
        String sumMoney = (String) model.getValueAt(row, 9);
        AirLine air = new AirLine();
        air.setAirLineNum(airLineNum);
        air.setFrom(from);
        air.setTo(to);
        air.setPrice(Double.parseDouble(price));
        air.setDiscountPrice(Double.parseDouble(discountPrice));
        air.setDepartureTime(departureTime);
        air.setSoldNum(Integer.parseInt(soldNum));
        air.setMax(Integer.parseInt(max));
        air.setDiscountNum(Integer.parseInt(discountNum));
        air.setSumMoney(Double.parseDouble(sumMoney));
        AirLineControl.insert(air);
    }
    public static void main(String args[]) {
        new AirLineManagerViewT();

    }

}

/**
 *
 * @author zhaoyingxi
 */
class Table_Model extends AbstractTableModel {

    private static final long serialVersionUID = -7495940408592595397L;

    private Vector content = null;

    private String[] title_name = { "AIRLINE NUM", "FROM", "TO", "PRICE", "DISCOUNT PRICE","DEPARTURE TIME","SOLD NUM","MAX NUM","DISCOUNT NUM",
    "SUM MONEY"};

    public Table_Model() {
        content = new Vector();
    }

    public Table_Model(int count) {
        content = new Vector(count);
    }

    public void addRow(String airlineNum,String from,String to,String price,String discountPrice,String depatureTime
                       ,String soldNum,String maxNum, String discountNum,String sumMoney) {
        Vector v = new Vector(4);
        v.add(0, airlineNum);
        v.add(1, from);
        v.add(2, to);
        v.add(3, price);
        v.add(4, discountPrice);
        v.add(5, depatureTime);
        v.add(6, soldNum);
        v.add(7, maxNum);
        v.add(8, discountNum);
        v.add(9, sumMoney);
        content.add(v);
    }

    public void removeRow(int row) {
        content.remove(row);
    }

    public void removeRows(int row, int count) {
        for (int i = 0; i < count; i++) {
            if (content.size() > row) {
                content.remove(row);
            }
        }
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    public void setValueAt(Object value, int row, int col) {
        ((Vector) content.get(row)).remove(col);
        ((Vector) content.get(row)).add(col, value);
        this.fireTableCellUpdated(row, col);
    }

    public String getColumnName(int col) {
        return title_name[col];
    }

    public int getColumnCount() {
        return title_name.length;
    }

    public int getRowCount() {
        return content.size();
    }

    public Object getValueAt(int row, int col) {
        return ((Vector) content.get(row)).get(col);
    }

    public Class getColumnClass(int col) {
        return getValueAt(0, col).getClass();
    }
}
